package com.notary.controller;


import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.notary.entity.vo.JsonResult;
import  com.notary.service.SysUserService;
import com.notary.entity.SysUser;

/**
* <p>
* 系统用户 前端控制器
* </p>
* @author xujun
* @since 2020-07-28
*/


@Api(tags = "系统用户控制器")
@RequestMapping("/sysUser")
@RestController
public class SysUserController{


    SysUserService sysUserService;

    @Autowired
    public  SysUserController(SysUserService sysUserService){
        this.sysUserService = sysUserService;
    }

    /**
    * <p>
        * 根据条件查询全部记录（并翻页）
        * </p>
    */

    @ApiOperation(value = "列表查询")
    @GetMapping("/list")
    //     @PreAuthorize("hasAnyAuthority(''sys:user:list')")
    public JsonResult<IPage<SysUser>> list(int pageNum, int pageSize,SysUser sysUser) {
        Page<SysUser> page = sysUserService.findAllAndPage(new Page<SysUser>(pageNum,pageSize),sysUser);
        return JsonResult.success(page);
    }


    /**
        *<P>
        * 根据ID查找返回单个对象
        *</P>
    */
    @ApiOperation(value = "根据id查询")
    @GetMapping("/find/{id}")
    //     @PreAuthorize("hasAnyAuthority(''sys:user:info')")
    public JsonResult<SysUser> getById(@PathVariable("id") Integer id) {
         return JsonResult.success(sysUserService.getById(id));
    }


    /**
        *<p>
        * 保存对象-如果对象存在则修改原数据，否则插入一条新数据
        *</p>
    */
    @ApiOperation(value = "保存对象")
    @PostMapping("/saveOrUpdate")
//     //     @PreAuthorize("hasAnyAuthority(''
    public JsonResult saveOrUpdate(@RequestBody SysUser sysUser) {
        sysUser.setPassword(SecureUtil.md5(sysUser.getPassword()));
        if(sysUser.getUserId()!=null){
        }
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("username",sysUser.getUsername());
        if(sysUserService.getOne(queryWrapper)!=null){
            return JsonResult.fail("名称重复");
        }
        sysUserService.saveOrUpdate(sysUser);
        return JsonResult.success(sysUser.getUserId());
    }


    /**
        *<p>
        * 根据 id删除记录-逻辑删除
        *</p>
    */
    @ApiOperation(value = "删除")
    @GetMapping("/remove/{id}")
    //     @PreAuthorize("hasAnyAuthority(''sys:user:delete')")
    public JsonResult removeById(@PathVariable("id") Integer id) {
        return JsonResult.success(sysUserService.removeById(id.longValue()));
    }

    /**
     *<P>
     * 修改密码
     *</P>
     */
    @ApiOperation(value = "修改密码")
    @PostMapping("/updatePassword")
    public JsonResult<SysUser> updatePassword(@RequestBody SysUser sysUser) {
        return sysUserService.updatePassword(sysUser);

    }


    /**
     *<P>
     * 修改账号头像
     *</P>
     */
    @ApiOperation(value = "修改密码")
    @PostMapping("/updateHeadImg")
    public JsonResult<SysUser> updateHeadImg(@RequestBody SysUser vo) {
        SysUser sysUser= sysUserService.getById(vo.getUserId());
        sysUser.setHeadImg(vo.getHeadImg());
         sysUserService.updateById(sysUser);
         return JsonResult.success();

    }
}

                       